/**
  ******************************************************************************
  * @file    main.c 
  * @author  Admin
  * @version V1.0.0
  * @date    2018-06-06
  * @brief   Main program body
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup Template_Project
  * @{
  */ 
#define task_recv can_recv_thread
#define task_send can_send_thread

extern void can_recv_thread(void *pdata);
extern void can_send_thread(void *pdata);

/* Task def ------------------------------------------------------------------*/

#define START_TASK_PRIO			( 10u )  //Task Start has the lowest prio
#define START_STK_SIZE			( 128u )
OS_STK  START_TASK_STK[START_STK_SIZE];
static void start_task(void *pdata);

#define TASK_PRIO_SEND			( 3u )  //Task Start has the lowest prio
#define STK_SIZE_SEND			( 512u )
OS_STK  STK_TASK_SEND[STK_SIZE_SEND];

#define TASK_PRIO_RECV			( 2u )  //Task Start has the lowest prio
#define STK_SIZE_RECV			( 512u )
OS_STK  STK_TASK_RECV[STK_SIZE_RECV];


#define FPGA_TASK_PRIO			( 6u )
#define FPGA_STK_SIZE			( 256u )
OS_STK  FPGA_TASK_STK[FPGA_STK_SIZE];

#define COMM_TASK_PRIO			( 4u )
#define COMM_STK_SIZE			( 128u )
OS_STK  COMM_TASK_STK[COMM_STK_SIZE];

#define HIGH_TASK_PRIO			( 1u )
#define HIGH_STK_SIZE			( 128u )
OS_STK  HIGH_TASK_STK[HIGH_STK_SIZE];


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
static void GPIO_Configuration(void);
static void NVIC_Configuration(void);
static void INIT_Configuration(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	/*!< At this stage the microcontroller clock setting is already configured, 
	   this is done through SystemInit() function which is called from startup
	   files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/
	   startup_stm32f429_439xx.s/startup_stm32f401xx.s or startup_stm32f411xe.s)
	   before to branch to application main.
	   To reconfigure the default setting of SystemInit() function, 
	   refer to system_stm32f4xx.c file */

	// Add your application code here

	// os initialize
	OSInit();

	// os start task create
	OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
	
	// os running
	OSStart(); 

}

/**
  * @brief  start task.
  * @param  none
  * @param  none
  * @retval None
  */
static void start_task(void *pdata)
{
	OS_CPU_SR cpu_sr = 0;
	pdata = pdata;
	
	// user block init 
	GPIO_Configuration();
	NVIC_Configuration();
	INIT_Configuration();

	// delay and systick init
	delay_init(168);

#if THINKROB_WDG_EN > 0
	fWdg.init();
#endif

	// os statistic task init
	OSStatInit();

	OS_ENTER_CRITICAL();
	
	// add user task 	
	OSTaskCreate( fpga_task,
				  (void*)0,
				  (OS_STK*)&FPGA_TASK_STK[FPGA_STK_SIZE-1],
				  FPGA_TASK_PRIO );	
	
	OSTaskCreate( comm_task,
				  (void*)0,
				  (OS_STK*)&COMM_TASK_STK[COMM_STK_SIZE-1],
				  COMM_TASK_PRIO );	
	

	
	OSTaskCreate( high_task,
				  (void*)0,
				  (OS_STK*)&HIGH_TASK_STK[HIGH_STK_SIZE-1],
				  HIGH_TASK_PRIO );
		

	OSTaskCreate( task_recv,
				  (void*)0,
				  (OS_STK*)&STK_TASK_RECV[STK_SIZE_RECV-1],
				  TASK_PRIO_RECV );	

	OSTaskCreate( task_send,
				  (void*)0,
				  (OS_STK*)&STK_TASK_SEND[STK_SIZE_SEND-1],
				  TASK_PRIO_SEND );
	// suspend start task
	OSTaskSuspend(START_TASK_PRIO);
	
	OSTimeSet(0);
	
	OS_EXIT_CRITICAL();
}

/**
  * @brief  the configuration of initialise.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
static void INIT_Configuration(void)
{		
	fLed.init();
	//fInput.input_init();
	//fInput.output_init();
	//fComm.init();		
	fGlobal.init();
	motorControl.init();
	KEY_Init();
	
	Fram_Init();
	Fram_Data_Init();
	tGlobal.InitFlag = 0;
	tGlobal.Init_addr= 0;
	tGlobal.Log_addr = 0;
}

/**
  * @brief  the configuration of GPIO rcc.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void GPIO_Configuration(void)
{
  	RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | 
							 RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | 
		                     RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF |
		                     RCC_AHB1Periph_GPIOG , ENABLE);

  	RCC_APB2PeriphClockCmd ( RCC_APB2Periph_SYSCFG , ENABLE);
}

/**
  * @brief  the configuration of NVIC.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void NVIC_Configuration(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

